package cn.wongshu.student.controller;

import cn.wongshu.student.pojo.Category;
import cn.wongshu.student.pojo.Message;
import cn.wongshu.student.pojo.Result;
import cn.wongshu.student.pojo.StatusCode;
import cn.wongshu.student.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value = "/category" )
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    //查看全部商品
    @GetMapping()
    public Result findAll(){
        List<Category> categoryList = categoryService.findAll();
        return new Result(true, StatusCode.Ok, Message.SELECT_SUCCESS,categoryList);

    }

    //新增一个分类
    @PostMapping("/save")
    public Result save(@RequestBody Category category){
        categoryService.save(category);
        return new Result(true,StatusCode.Ok,Message.ADD_SUCCESS);
    }

    //根据主键查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable(name = "id") Integer id){
        Category category = categoryService.findById(id);
        return new Result(true,StatusCode.Ok,Message.SELECT_SUCCESS,category);
    }

    //条件查询
    @PostMapping(value = "/search")
    public Result search(@RequestBody Category category){
       List<Category> categoryList =  categoryService.search(category);
       return new Result(true,StatusCode.Ok,Message.SELECT_SUCCESS,categoryList);
    }


    //删除
    @PostMapping("/delete")
    public Result deleteByIds(@RequestBody Integer[] ids){

        if (ids == null || ids.length == 0){
            throw new RuntimeException("请至少选择一项");
        }
        categoryService.deleteByIds(ids);
        return new Result(true,StatusCode.Ok,Message.DELETE_SUCCESS);

    }

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Category category){
        categoryService.update(category);
        return new Result(true,StatusCode.Ok,Message.UPDATE_SUCCESS);
    }


    @GetMapping("/getLevel")
    public Result getLevel(){
       int count = categoryService.getLevel();
       return new Result(true,StatusCode.Ok,Message.SELECT_SUCCESS,count);

    }



}
